﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Linq;
using CarbonExchange.Bll;

namespace CarbonExchange.Tests
{
    [TestClass]
    public class SiteTests : BaseTest
    {
        [TestMethod]
        public void SiteGetByIdReturnsNullIfIncorrectId()
        {
            Assert.IsNull(new Bll.SiteFactory(_Context).GetById(int.MaxValue));
        }

        [TestMethod]
        public void SiteGetByIdReturnsNotNullIfCorrectId()
        {
            Assert.IsNotNull(new Bll.SiteFactory(_Context).GetById(_TestSite.Id));
        }

        [TestMethod]
        public void SiteGetByIdReturnesCorrectProperties()
        {
            Site site = new Bll.SiteFactory(_Context).GetById(_TestSite.Id);
            Assert.AreEqual(_TestSite.Id, site.Id);
            Assert.AreEqual("Test Site", site.Name);
            Assert.AreEqual("localhost/testing1", site.RootUrl);
            Assert.AreEqual(DateTime.Parse("24/10/1975 12:00:00"), site.Created);   

        }

        [TestMethod]
        public void SiteGetByPathReturnsNullWithIncorectPath()
        {
            Site site = new Bll.SiteFactory(_Context).GetByPath("rubbish");
            Assert.IsNull(site);
        }

        [TestMethod]
        public void SiteGetByPathReturnsNullWithDefaultPath()
        {
            Site site = new Bll.SiteFactory(_Context).GetByPath("localhost/testing1");
            Assert.IsNotNull(site);
            Assert.AreEqual(_TestSite.Id, site.Id);
            Assert.AreEqual("Test Site", site.Name);
            Assert.AreEqual("localhost/testing1", site.RootUrl);
            Assert.AreEqual(DateTime.Parse("24/10/1975 12:00:00"), site.Created);
        }

        [TestMethod]
        public void SiteGetByPathReturnsNullWithNotDefaultPath()
        {
            Site site = new Bll.SiteFactory(_Context).GetByPath("localhost/testing2");
            Assert.IsNotNull(site);
            Assert.AreEqual(_TestSite.Id, site.Id);
            Assert.AreEqual("Test Site", site.Name);
            Assert.AreEqual("localhost/testing1", site.RootUrl);
            Assert.AreEqual(DateTime.Parse("24/10/1975 12:00:00"), site.Created);
        }

        [TestMethod]
        public void SiteGetByPathReturnsNullWithCaseInsesitivePath()
        {
            Site site = new Bll.SiteFactory(_Context).GetByPath("localhost/testing2".ToUpper());
            Assert.IsNotNull(site);
            Assert.AreEqual(_TestSite.Id, site.Id);
            Assert.AreEqual("Test Site", site.Name);
            Assert.AreEqual("localhost/testing1", site.RootUrl);
            Assert.AreEqual(DateTime.Parse("24/10/1975 12:00:00"), site.Created);
        }

        [TestMethod]
        public void SiteInsertTest()
        {
            Site tmpSite = new Site();
            tmpSite.Name = "Create New Test";
            tmpSite.RootUrl = "localhost/CreateNewTest";

            DateTime expectedDateTime = DateTime.Now;
            new Bll.SiteFactory(_Context).Save(tmpSite);

            Assert.IsTrue(tmpSite.Id > 0);
            Assert.IsTrue(tmpSite.Created - expectedDateTime <= TimeSpan.FromSeconds(1));

            Site tmp2 = new Bll.SiteFactory(_Context).GetById(tmpSite.Id);
            Assert.AreEqual(tmpSite.Id, tmp2.Id);
            Assert.AreEqual(tmpSite.Name, tmp2.Name);
            Assert.AreEqual(tmpSite.RootUrl, tmp2.RootUrl);
            Assert.AreEqual(tmpSite.Created, tmp2.Created);

        }


        [TestMethod]
        public void SiteDeleteTest()
        {
            SiteFactory factory = new SiteFactory(_Context);
            Site tmpSite = factory.GetById(_TestSite.Id);
            Assert.IsNotNull(tmpSite);
            
            factory.Delete(tmpSite);

            tmpSite = factory.GetById(_TestSite.Id);
            Assert.IsNull(tmpSite);

            var dalSite = _Context.Sites.Where(x=> x.Id == _TestSite.Id).Single();

            Assert.IsTrue(dalSite.Deleted);

            var dalSiteRoots = _Context.SiteRoots.Where(x => x.SiteId == _TestSite.Id).Select(x => x);
            Assert.IsTrue(dalSiteRoots.Count() > 0);
            foreach(var i in dalSiteRoots)
            {
                Assert.IsTrue(i.Deleted);
            }

        }
    }
}
